/**
 * 
 */
package cn.bblocks.cacheTest.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.support.Page4Navigator;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.bblocks.cacheTest.exceptions.BusinessException;
import cn.bblocks.cacheTest.model.User;
import cn.bblocks.cacheTest.model.UserBank;
import cn.bblocks.cacheTest.service.AreaService;
import cn.bblocks.cacheTest.service.UserBankService;
import cn.bblocks.cacheTest.service.UserService;
import cn.bblocks.cacheTest.suport.RandomValue;
import cn.bblocks.cacheTest.suport.dto.response.Response;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/** 
* @version:v1.0
* @Description: 典型的用户银行卡 查询，增、删、改
* @author: jzhao
* @date: 2019年12月19日上午9:42:18
*/
@Slf4j
@RestController
@RequestMapping(value="/userBank")
public class UserBankController {
	@Autowired
	private AreaService areaService;
	@Autowired
	private UserBankService userBankService;
	@Autowired
	private UserService userService;
	
	
	@ApiOperation(value = "用户卡随机+")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "loginName", value = "loginName", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Response userBankAdd(String loginName) {
		
		User user = userService.selectByLoginName(loginName);

		UserBank onebank = new UserBank();
		onebank.setIdcardseqno(user.getId());
		onebank.setCreateTime(new Date());
	
		onebank.setMobile(RandomValue.getTel());
		//62266609 0168 3439
		onebank.setNum("62266609"+RandomValue.getNum(1000, 10000) + RandomValue.getNum(1000, 10000));
		onebank.setRealName(user.getName());
		onebank.setType("xx银行");
		onebank.setStatus(1);
		
		userBankService.add(onebank);
		

		return Response.getInstance( "OK" );
	} 
	
	@ApiOperation(value = "用户银行卡修改(非核心)")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	public Response<UserBank> userBankInfoUpdate(Integer id) {
		UserBank bank = userBankService.getBank(id);
		bank.setModifyTime(new Date());
		int pre = bank.getVersion();
		bank = userBankService.update(bank);
//		int after = bank.getVersion();
//		UserBank cUserBank = userBankService.getBank2(bank.getId());
//		int c = cUserBank.getVersion();
//		if(!cUserBank.getVersion().equals(after)) {
//			
//			throw new BusinessException("ID:"+bank.getId()+" 更新完版本不一致,之前:"+pre+" 之后："+after+" 实际："+c);
//		}
		

		return Response.getInstance( bank );
	} 
	
	@ApiOperation(value = "用户银行卡删除（类似核心修改）")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true,paramType = "query" ),
     })
	@RequestMapping(value = "/del", method = RequestMethod.DELETE)
	public Response userBankInfoDel(Integer id) {
		UserBank bank = userBankService.getBank(id);
		if(bank != null)
			userBankService.deleteByPrimaryKey(bank);
		
		return Response.getInstance( "" );
	} 
	
	
	@ApiOperation(value = "用户全部银行卡")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "loginName", value = "loginName", required = true,paramType = "query",defaultValue="15201080314"  ),
     })
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public Response<List<UserBank>> userBanks(String loginName) {
		User user = userService.selectByLoginName(loginName);
		
		if(user != null) {
			//System.out.println(b);
			//log.info("loginName:{}",loginName);
			List<UserBank> list = userBankService.getMyBanks(user.getId());
			return Response.getInstance( list );
		}else {
			
			return Response.nonExpect(1);
		}
		//return Response.getInstance( userBankService.getMyBanks(1) );
		
//		return Response.getInstance( my );
	} 
	
	@ApiOperation(value = "用户Page银行卡")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "loginName", value = "loginName", required = true,paramType = "query",defaultValue="15201080314"  ),
     })
	@RequestMapping(value = "/onePage", method = RequestMethod.GET)
	public Response<Page4Navigator<UserBank>> userPageBanks(String loginName
			,@PageableDefault(sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable) {
		User user = userService.selectByLoginName(loginName);
		
		if(user != null) {
			return Response.getInstance( userBankService.getMyPageBanks(user.getId(),pageable) );
		}else {
			
			return Response.nonExpect(1);
		}
		//return Response.getInstance( userBankService.getMyBanks(1) );
		
//		return Response.getInstance( my );
	} 
	
}
